﻿//                        ElgProg
//
//    A cheap DIY Arduino based Universal Programmer
//
//    Copyright (C) Elgen };-P aka Morten Overgaard 2015
//
//    This file is part of ElgProg.
//
//    ElgProg is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    ElgProg is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with ElgProg. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.IO;
using System.IO.Ports;

namespace ElgProg
{
  class Cmd
  {
    static int fileBufferSize = 65536;
    static byte[] reciveBuffer = new byte[fileBufferSize];
    static string comPortName = "COM1";
    static int baudRate = 115200;

    static void Main(string[] args)
    {
      var start = DateTime.Now;
      var serialPort = new SerialPort(comPortName, baudRate);
      serialPort.ReadBufferSize = fileBufferSize;
      serialPort.Open();
      System.Threading.Thread.Sleep(1000);
      Console.WriteLine("Asking who");
      serialPort.WriteLine("who");
      while (serialPort.BytesToRead == 0) { }
      var got = serialPort.ReadLine();
      Console.WriteLine("Got: " + got);

      if (got != "EPROM")
      {
        serialPort.Close();
        ArduinoTools.UploadSketchHex("Eprom", comPortName);
        serialPort = new SerialPort(comPortName, baudRate);
        serialPort.ReadBufferSize = fileBufferSize;
        serialPort.Open();
      }

      Console.WriteLine("Sending configuration");
      SendEPROMConfiguration(serialPort);
      while (serialPort.BytesToRead == 0) { }
      got = serialPort.ReadLine();
      Console.WriteLine("Got: " + got);
      //Console.WriteLine("Sending verify command");
      //serialPort.Write("verify");
      Console.WriteLine("Sending read command");
      serialPort.Write("read");
      Console.WriteLine("Waiting!");
      while (serialPort.BytesToRead == 0) { }
      Console.WriteLine("Reading bytes...");
      var index = 0;
      while (index < fileBufferSize)
      {
        if (serialPort.BytesToRead != 0)
        {
          index += serialPort.Read(reciveBuffer, index, fileBufferSize - index);
        }
      }
      Console.WriteLine("Bytes read: {0}", index);

      using (var stream = File.OpenWrite(args[0]))
      {
        stream.Write(reciveBuffer, 0, fileBufferSize);
      }
      Console.WriteLine("Bytes written to: " + args[0]);
      Console.WriteLine("Elapsed time: " + (DateTime.Now - start));
    }

    //                       A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15
    static byte[] E27512 = { 40, 38, 36, 34, 32, 30, 28, 26, 29, 31,  37,  33,  24,  27,  25,  22, 0,
    //                       D0, D1, D2, D3, D4, D5, D6, D7
                             42, 44, 46, 49, 47, 45, 43, 41, 0,
    //                       CE
                             39, 0,
    //                       OE
                             35, 0 };

    //                       A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14
    static byte[] E27256 = { 40, 38, 36, 34, 32, 30, 28, 26, 29, 31,  37,  33,  24,  27,  25,  0,
    //                       D0, D1, D2, D3, D4, D5, D6, D7
                             42, 44, 46, 49, 47, 45, 43, 41, 0,
    //                       CE
                             39, 0,
    //                       OE
                             35, 0 };

    static void SendEPROMConfiguration(SerialPort serialPort)
    {
      serialPort.Write("conf\n");
      while (serialPort.BytesToWrite > 0) { }
      serialPort.Write(E27512, 0, E27512.Length);
    }
  }
}
